<template>

  <el-dialog
    class="assign-role"
    title="分配角色"
    :visible="showRoleDialog"
    width="40%"
    @open="open"
    @close="close"
  >
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in rolslist" :key="item.id" v-loading="loading" :label="item.id">{{ item.name }}</el-checkbox>

    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="assign">确定</el-button>
      <el-button size="small" @click="close">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getallroleres } from '@/api/setting'
import { getuserallinfoapi } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      rolslist: [],
      checkList: [],
      loading: false
    }
  },
  created() {
    this.getallrole()
  },
  methods: {
    async getallrole() {
      const res = await getallroleres(1, 100)
      this.rolslist = res.rows
    },
    async open() {
      this.loading = true
      const res = await getuserallinfoapi(this.userId)
      this.checkList = res.roleIds
      this.loading = false
    },
    // 关闭弹窗
    close() {
      this.$emit('offshow')
    },
    // 确定更新用户角色
    async assign() {
      await assignRoles({
        id: this.userId,
        roleIds: this.checkList
      })
      this.$message.success('分配角色成功')
      this.close()
    }

  }
}
</script>
